
const app = getApp()

Page({
  data: {
    click_button: 0,
    phone: '',
    code: '',
    password: '',
    password2: '',
    send: true,
    alreadySend: false,
    second: 60,
    checked: false
  },
  
  onShow:function(){
    var that=this;
    // that.getOpenid();
  },

  checkedTap:function(e){
    this.setData({
      checked: !this.data.checked
    })
  },

  xieyi:function(e){
    wx.showToast({
      title: '《云数用户协议》',
      icon: 'none',
      duration: 2000,
    })
  },

  zhengce:function(e){
    wx.showToast({
      title: '《云数隐私政策》',
      icon: 'none',
      duration: 2000,
    })
  },

  submit_result:function(){
    var that = this;
    if (that.data.phone == "") {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
        duration: 2000,
      })
    } else if (!(/^1(3|4|5|7|8|9)\d{9}$/.test(that.data.phone))) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000,
      })
    } else if (!(that.data.code)) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: 2000,
      })
    } else if (!(that.data.password)) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        duration: 2000,
      })
    } else if(that.data.password != that.data.password2){
      console.log(that.data)
      wx.showToast({
        title: '两次密码输入不一致',
        icon: 'none',
        duration: 2000,
      })
    }else if (that.data.checked == false) {
      wx.showToast({
        title: '请同意《云数用户协议》和《云数隐私政策》',
        icon: 'none',
        duration: 2000,
      })
    } else {
      this.setData({
        click_button: 1
      })
      wx.request({
        url: app.globalData.domain + 'tea/register_yonghu/',
        method: "POST",
        data: {
          username: that.data.phone,
          code: that.data.code,
          password1: that.data.password,
          password2: that.data.password2
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function(res) {
          if (res.data.response == "ok") {
            wx.showToast({
              title: '注册成功！',
              icon: 'success',
              duration: 2000,
              success: function() {
                that.setData({
                  phone: '',
                  code: '',
                  password:'',
                  click_button: 0,
                  send: true,
                  alreadySend: false,
                  second: 60
                })
                app.setToken(res.data.data.Token)
                app.setUserId(res.data.data.user_id)
                setTimeout(() => {
                  wx.navigateTo({
                    url: '/pages/center/index',
                  })
                }, 2000);
              }
              
            })
          } else{
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000,
              success: function() {
                //...
              }
            })
          } 
        }
      })
    }
  },

  //获取openid
  getOpenid: function () {
    var that = this;
    wx.login({
      success: function (myres) {
        console.log(myres.code)
        wx.request({
          url: app.globalData.domain + 'index/getOpenid/',
          method: "POST",
          data: {
            code: myres.code,
          },
          header: {
            "Content-Type" : "application/x-www-form-urlencoded"
          },
          success: function (res) {
            wx.setStorageSync("openid", res.data.data.openid);
          }
        })
      }
    })
  },

  getPhone: function (e) {
    this.setData({
      phone: e.detail.value,
    })
  },
  getCode: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  getPassword: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  getPassword2: function (e) {
    this.setData({
      password2: e.detail.value
    })
  },
  sendMsg: function (options) {
    var that=this;
    if(that.data.phone==""){
      wx.showToast({
        title: '请填写您的手机号码',
        icon: 'none',
        duration: 2000,
      })
    }else{
      if ((/^1(3|4|5|7|8|9)\d{9}$/.test(that.data.phone))) {
        wx.request({
          url: app.globalData.domain + 'sms/send/',
          method: "POST",
          data: {
            phone: that.data.phone,
            code_id: 4
            // openid: wx.getStorageSync('openid')
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            if(res.data.response == "fail"){
              wx.showToast({
                title: res.data.message,
                icon: 'none',
                duration: 2000
              })
            }else{
              that.setData({
                alreadySend: true,
                send: false
              })
              that.timer();
              wx.showToast({
                title: res.data.message,
                icon: 'success',
                duration: 2000
              })
            }
          }
        })
      }else{
        wx.showToast({
          title: '请填写正确的手机号码',
          icon: 'error',
          duration: 2000,
        })
      }
    }
  },

  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          this.setData({
            second: this.data.second - 1
          })
          if (this.data.second <= 0) {
            this.setData({
              second: 60,
              alreadySend: false,
              send: true
            })
            resolve(setTimer)
          }
        }
        , 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },

  goLogin : function () {
    wx.navigateTo({
      url: '/pages/center/login',
    })
  }

})
